package web.java.listener;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineListener implements HttpSessionListener{

	@Override
	public void sessionCreated(HttpSessionEvent arg0) {
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent arg0) {
		//每次退出用户，都会销毁session，在销毁session时，将用户从list中移除
		HttpSession session=arg0.getSession();
		ServletContext sc=session.getServletContext();
		List<String> onlineUserList=(List<String>)sc.getAttribute("onlineUserList");
		String username=(String)session.getAttribute("username");
		if(onlineUserList.contains(username)){
			onlineUserList.remove(username);
		}
	}

}
